package com.paiai.lslease.service.landlordauth;

import com.paiai.lslease.auto.dao.AppLandlordAuthDAO;
import com.paiai.lslease.auto.model.AppLandlordAuth;
import com.paiai.lslease.auto.model.AppLandlordAuthExample;
import com.paiai.lslease.auto.model.AppSysUser;
import com.paiai.lslease.dao.landlordauth.AppLandlordAuthExternalDAO;
import com.paiai.lslease.model.AppLandlordAuthHouseInfo;
import com.paiai.lslease.service.appuser.AppSysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("appLandlordAuthService")
public class AppLandlordAuthServiceImpl implements AppLandlordAuthService {

    private static final Logger logger = LoggerFactory.getLogger(AppLandlordAuthServiceImpl.class);
    @Autowired
    private AppLandlordAuthDAO appLandlordAuthDAO;
    @Autowired
    private AppSysUserService appSysUserService;
    @Autowired
    private AppLandlordAuthExternalDAO appLandlordAuthExternalDAO;

    @Override
    public List<AppLandlordAuthHouseInfo> insertAppLandlordAuthList(List<AppLandlordAuth> appLandlordAuthList) throws Exception {
        //保存授权信息
        for (AppLandlordAuth appLandlordAuth : appLandlordAuthList) {
            appLandlordAuthDAO.insert(appLandlordAuth);
        }
        //查询当前手机号码对应的用户
        AppSysUser appSysUser = appSysUserService.getAppSysUserInfoByMobile(appLandlordAuthList.get(0).getUserMobile());
        if (appSysUser != null) {//用户已注册，修改用户的角色为房东授权的管理员
            if (!appSysUserService.addLandlordAuthManagerRoleToUserByMobile(appSysUser.getMobile())) {
                logger.info("用户已注册，修改用户的角色为房东授权的管理员失败");
                return null;
            }
        }
        return selectAppLandlordAuthListByUserMobile(appLandlordAuthList.get(0).getUserMobile());
    }

    @Override
    public AppLandlordAuth selectAppLandlordAuth(int landlordAuthId) throws Exception {
        return appLandlordAuthDAO.selectByPrimaryKey(landlordAuthId);
    }

    @Override
    public List<AppLandlordAuthHouseInfo> selectAppLandlordAuthListByLandlordId(String landlordId) throws Exception {
        return appLandlordAuthExternalDAO.selectAuthedHouseInfoListByLandlordId(Integer.parseInt(landlordId));
    }

    @Override
    public List<AppLandlordAuthHouseInfo> selectAppLandlordAuthListByUserMobile(String userMobile) throws Exception {
        return appLandlordAuthExternalDAO.selectAuthedHouseInfoListByUserMobile(userMobile);
    }

    @Override
    public List<AppLandlordAuthHouseInfo> selectAppLandlordAuthListByUserId(int userId) throws Exception {
        AppSysUser appSysUser = appSysUserService.getAppSysUserInfoById(userId);
        if (appSysUser == null) {
            return null;
        }
        return selectAppLandlordAuthListByUserMobile(appSysUser.getMobile());
    }

    @Override
    public boolean userHasAuthedHouse(String userMobile, int houseId) throws Exception {
//        List<AppLandlordAuthHouseInfo> appLandlordAuthList = selectAppLandlordAuthListByUserMobile(userMobile);
//        if (appLandlordAuthList == null) {
//            return false;
//        }
//        for (AppLandlordAuthHouseInfo appLandlordAuth : appLandlordAuthList) {
//            if (userMobile.equalsIgnoreCase(appLandlordAuth.getAuthedHouseMangerMobile())  && houseId == appLandlordAuth.getId()) {
//                return true;
//            }
//        }
        AppLandlordAuthExample appLandlordAuthExample =  new AppLandlordAuthExample();
        appLandlordAuthExample.createCriteria().andHuoseIdEqualTo(houseId).andUserMobileEqualTo(userMobile).andItem1EqualTo("1");
        List<AppLandlordAuth> appLandlordAuthList = appLandlordAuthDAO.selectByExample(appLandlordAuthExample);
        return appLandlordAuthList != null && appLandlordAuthList.size() > 0;
    }

    @Override
    public boolean deleteAuthedHouse(String userMobile, int houseId) throws Exception {
        AppLandlordAuthExample appLandlordAuthExample =  new AppLandlordAuthExample();
        appLandlordAuthExample.createCriteria().andHuoseIdEqualTo(houseId).andUserMobileEqualTo(userMobile).andItem1EqualTo("1");
        AppLandlordAuth appLandlordAuth = new AppLandlordAuth();
        appLandlordAuth.setItem1("-1");
        appLandlordAuth.setUpdateTime(new Date());
        appLandlordAuthDAO.updateByExampleSelective(appLandlordAuth, appLandlordAuthExample);
        return true;
    }

    @Override
    public boolean deleteAppLandlordAuth(int appLandlordAuthId) throws Exception {
        //获取授权详情
        AppLandlordAuth appLandlordAuth_ = appLandlordAuthDAO.selectByPrimaryKey(appLandlordAuthId);
        if (appLandlordAuth_ == null) {
            logger.info("获取授权详情失败");
            return false;
        }
        //查询用户的所有授权列表
        AppLandlordAuthExample appLandlordAuthExample = new AppLandlordAuthExample();
        appLandlordAuthExample.createCriteria().andUserMobileEqualTo(appLandlordAuth_.getUserMobile()).andItem1EqualTo("1");
        List<AppLandlordAuth> appLandlordAuthList = appLandlordAuthDAO.selectByExample(appLandlordAuthExample);
        //删除用户的最后一个授权时，删除用户的房东授权管理员角色
        if (appLandlordAuthList == null || appLandlordAuthList.size() == 0) {
            logger.info("获取授权详情列表失败");
            return false;
        }
        if (appLandlordAuthList.size() == 1 && !appSysUserService.deleteLandlordAuthManagerRoleToUserByMobile(appLandlordAuth_.getUserMobile())) {
            logger.info("删除用户的最后一个授权时，删除用户的房东授权管理员角色失败");
            return false;
        }
        AppLandlordAuth appLandlordAuth = new AppLandlordAuth();
        appLandlordAuth.setId(appLandlordAuthId);
        appLandlordAuth.setItem1("-1");
        appLandlordAuth.setUpdateTime(new Date());
        return appLandlordAuthDAO.updateByPrimaryKeySelective(appLandlordAuth) > 0;
    }

    @Override
    public List<AppSysUser> queryAllAuthedManager(int landlordId) throws Exception {
        return appLandlordAuthExternalDAO.selectAllAuthedManager(landlordId);
    }

    @Override
    public List<AppLandlordAuth> queryAppLandlordAuthListByUserMobile(String userMobile) throws Exception {
        AppLandlordAuthExample appLandlordAuthExample = new AppLandlordAuthExample();
        appLandlordAuthExample.createCriteria().andUserMobileEqualTo(userMobile).andItem1EqualTo("1");
        return appLandlordAuthDAO.selectByExample(appLandlordAuthExample);
    }

    @Override
    public boolean deleteAuthedManager(String userMobile) throws Exception {
        AppLandlordAuthExample appLandlordAuthExample = new AppLandlordAuthExample();
        appLandlordAuthExample.createCriteria().andUserMobileEqualTo(userMobile).andItem1EqualTo("1");
        AppLandlordAuth appLandlordAuth = new AppLandlordAuth();
        appLandlordAuth.setItem1("-1");
        appLandlordAuth.setUpdateTime(new Date());
        return appLandlordAuthDAO.updateByExampleSelective(appLandlordAuth, appLandlordAuthExample) >= 0 && appSysUserService.deleteLandlordAuthManagerRoleToUserByMobile(userMobile);
    }
}
